using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using SelfMadeMillionaire.BLL.Services;
using SelfMadeMillionaire.Core.Repositories;
using SelfMadeMillionaire.Core.Services;
using SelfMadeMillionaire.DAL;
using SelfMadeMillionaire.DAL.Repositories;
using SelfMadeMillionaire.Web.App_Start;
using WebActivator;

[assembly: WebActivator.PreApplicationStartMethod(typeof (NinjectWebCommon), "Start")]
[assembly: ApplicationShutdownMethod(typeof (NinjectWebCommon), "Stop")]

namespace SelfMadeMillionaire.Web.App_Start
{
	public static class NinjectWebCommon
	{
		private static readonly Bootstrapper bootstrapper = new Bootstrapper();

		/// <summary>
		///     Starts the application
		/// </summary>
		public static void Start()
		{
			DynamicModuleUtility.RegisterModule(typeof (OnePerRequestHttpModule));
			DynamicModuleUtility.RegisterModule(typeof (NinjectHttpModule));
			bootstrapper.Initialize(CreateKernel);
		}

		/// <summary>
		///     Stops the application.
		/// </summary>
		public static void Stop()
		{
			bootstrapper.ShutDown();
		}

		/// <summary>
		///     Creates the kernel that will manage your application.
		/// </summary>
		/// <returns>The created kernel.</returns>
		private static IKernel CreateKernel()
		{
			var kernel = new StandardKernel();
			kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
			kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

			RegisterServices(kernel);
			return kernel;
		}

		/// <summary>
		///     Load your modules or register your services here!
		/// </summary>
		/// <param name="kernel">The kernel.</param>
		private static void RegisterServices(IKernel kernel)
		{
			kernel.Bind<IPublicationService>().To<PublicationService>();
            kernel.Bind<IPublicationProductService>().To<PublicationProductService>();
			kernel.Bind<INewsletterService>().To<NewsletterService>();
            kernel.Bind<INewsletterProductService>().To<NewsletterProductService>();
            kernel.Bind<INewsletterReadService>().To<NewsletterReadService>();
            kernel.Bind<INewsService>().To<NewsService>();
            kernel.Bind<INewsProductService>().To<NewsProductService>();
			kernel.Bind<IMailingListService>().To<MailingListService>();
			kernel.Bind<ITrackingPortfolioService>().To<TrackingPortfolioService>();
			kernel.Bind<IEmailService>().To<EmailService>();
			kernel.Bind<ISubscriberService>().To<SubscriberService>();
			kernel.Bind<IPaymentService>().To<PaymentService>();
			kernel.Bind<ISchedulerService>().To<SchedulerService>();
            kernel.Bind<IPromotionService>().To<PromotionService>();
            kernel.Bind<ILogService>().To<LogService>();
            kernel.Bind<ILogVisitService>().To<LogVisitService>();
            kernel.Bind<IEmailSendService>().To<EmailSendService>();
            kernel.Bind<IReportsService>().To<ReportsService>();
            kernel.Bind<IUserPortfolioService>().To<UserPortfolioService>();
            kernel.Bind<IUserSymbolService>().To<UserSymbolService>();
            kernel.Bind<ISymbolOptionsHistoryService>().To<SymbolOptionsHistoryService>();
            //kernel.Bind<IMailingOpenLogService>().To<MailingOpenLogService>();

			kernel.Bind<IPublicationRepository>().To<PublicationRepository>();
            kernel.Bind<IPublicationProductRepository>().To<PublicationProductRepository>();
			kernel.Bind<INewsletterRepository>().To<NewsletterRepository>();
            kernel.Bind<INewsletterProductRepository>().To<NewsletterProductRepository>();
            kernel.Bind<INewsletterReadRepository>().To<NewsletterReadRepository>();
            kernel.Bind<INewsRepository>().To<NewsRepository>();
            kernel.Bind<INewsProductRepository>().To<NewsProductRepository>();
			kernel.Bind<IEmailMessageRepository>().To<EmailMessageRepository>();
			kernel.Bind<ITrackingPortfolioRepository>().To<TrackingPortfolioRepository>();
			kernel.Bind<ISubscriberRepository>().To<SubscriberRepository>();
			kernel.Bind<IPayPalPaymentRepository>().To<PayPalPaymentRepository>();
			kernel.Bind<IPayPalBuyerRepository>().To<PayPalBuyerRepository>();
			kernel.Bind<IPayPalTransactionRepository>().To<PayPalTransactionRepository>();
			kernel.Bind<IOrderRepository>().To<OrderRepository>();
			kernel.Bind<IUnitOfWork>().To<DataContext>().InRequestScope();
			kernel.Bind<ISchedulerRepository>().To<SchedulerRepository>();
            kernel.Bind<IPromotionRepository>().To<PromotionRepository>();
            kernel.Bind<ILogRepository>().To<LogRepository>();
            kernel.Bind<ILogVisitRepository>().To<LogVisitRepository>();
            kernel.Bind<IEmailSendRepository>().To<EmailSendRepository>();
            kernel.Bind<IEmailMessageSendRepository>().To<EmailMessageSendRepository>();
            kernel.Bind<IUserPortfolioRepository>().To<UserPortfolioRepository>();
            kernel.Bind<IUserSymbolRepository>().To<UserSymbolRepository>();
            kernel.Bind<ISymbolOptionsHistoryRepository>().To<SymbolOptionsHistoryRepository>();
            //kernel.Bind<IMailingOpenLogRepository>().To<MailingOpenLogRepository>();
		}
	}
}